package com.lab303.powerproject.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ClassUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

public class YieldPredictData {
      private static final  String _NAME = "\\predictweath_data.py";
      private static final String N_NAME = "\\ycnl.py";
      private static final String D_NAME = "\\ycdl.py";
      private static final Logger LOG = LoggerFactory.getLogger(YieldPredictData.class);

      public void startPredict(){
          String dir = ClassUtils.getDefaultClassLoader().getResource("").getPath();

          LOG.info(dir);
          File  path =new File(dir + "\\3rd\\aj\\code");
          String exe = "python",command = path.getAbsolutePath() + _NAME;
          String[] cmdArr = new String[]{exe,command};
          if(!path.exists()){
              LOG.info("预测脚本不存在!!!");
              System.exit(1);
          }else{

              try {
                  Process proc = Runtime.getRuntime().exec(cmdArr);// 执行py文件
                  LOG.info(cmdArr[1]);
                  if(proc.waitFor() != 0 ){
                      LOG.error(_NAME + "脚本执行异常!");
                  }

                  cmdArr[1] = path.getAbsolutePath() + D_NAME;
                  Process proc1 = Runtime.getRuntime().exec(cmdArr);
                  if(proc1.waitFor() != 0 ){
                      LOG.error(D_NAME + "脚本执行异常!");
                  }

                  cmdArr[1] =  path.getAbsolutePath() + N_NAME;
                  Process proc2 = Runtime.getRuntime().exec(cmdArr);
                  if(proc2.waitFor() != 0 ){
                      LOG.error(N_NAME + "脚本执行异常!");
                  }
              } catch (IOException e) {
                  LOG.error(e.getMessage());
                  e.printStackTrace();
              } catch (InterruptedException  e) {
                  LOG.error(e.getMessage());
                  e.printStackTrace();
              }
          }
      }

    public static void main(String[] args) {
        YieldPredictData yp =new YieldPredictData();
        yp.startPredict();
    }
}
